package at.fhj.swd.pse.gui.pageobjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import at.fhj.swd.pse.gui.drivers.GuiTestBaseSeleniumJava4Webdriver;

/**
 * 
 * @author Martin Steinbauer
 *
 */
public class NewsPage extends GuiTestBaseSeleniumJava4Webdriver {
	private final GuiTestBaseSeleniumJava4Webdriver webdriver = new GuiTestBaseSeleniumJava4Webdriver();
	
	public NewsPage() throws Exception {
		this.open();
	}
	
	public void open() throws Exception {
		this.webdriver.start();
		this.webdriver.getDriver().get(this.webdriver.getBaseUrl() + "/news.xhtml");
		this.webdriver.getDriver().findElement(By.id("newsoverview:createnews")).click();
	}
	
	public void close() throws Exception {
		this.webdriver.end();
	}
	
	public boolean createNewNews(String title, String content) {
		this.webdriver.getDriver().findElement(By.id("newscreate:title")).clear();
		this.webdriver.getDriver().findElement(By.id("newscreate:title")).sendKeys(title);
		this.webdriver.getDriver().findElement(By.id("newscreate:content")).clear();
		this.webdriver.getDriver().findElement(By.id("newscreate:content")).sendKeys(content);
		this.webdriver.getDriver().findElement(By.id("newscreate:createButton")).click();
		
		WebElement dynamicElement = new WebDriverWait(this.webdriver.getDriver(), 10).until(ExpectedConditions.presenceOfElementLocated(By.id("newsoverview:newslist")));
		return dynamicElement.getText().contains(title);
	}
}
